#include "LAnimationAssist.h"
#include "LMath.h"
#include <math.h>
#include <Windows.h>

namespace ln
{

    LAnimationAssist::LAnimationAssist(AnimationValFun fun /*= FunCalcHalfS4*/)
        :
        m_is_stop(true),
        m_start_value(0),
        m_end_value(0),
        m_time_ms(0),
        m_start_time(0),
        m_val_fun(fun)
    {
    }

    LAnimationAssist::~LAnimationAssist(void)
    {
    }


    double LAnimationAssist::ExpenseTime()
    {

        int x = GetTickCount() - m_start_time;
        if (x >= m_time_ms) {
            Stop();
            return m_end_value;
        }

        
        if (ln::FloatEque(m_start_value, m_end_value)) {
            Stop();
            return m_end_value;
        }

        double y;

        if (m_val_fun) {
            y = m_val_fun(x*1.0/m_time_ms);
        } else {
            y = x*1.0/m_time_ms;
        }


        double value = m_start_value + (m_end_value-m_start_value)*y;

        if (m_end_value > m_start_value && value > m_end_value) {
            Stop();
            return m_end_value;
        }

        if (m_end_value < m_start_value && value < m_end_value) {
            Stop();
            return m_end_value;
        }
        return value;
    }

    bool LAnimationAssist::IsStop() const
    {
        return m_is_stop;
    }

    void LAnimationAssist::Stop()
    {
        m_is_stop = true;
    }

    void LAnimationAssist::Start(double v1, double v2, __int64 time_ms)
    {
        m_is_stop = false;
        m_start_value = v1;
        m_end_value = v2;
        m_time_ms = time_ms;

        m_start_time = GetTickCount();
    }
}